gitPath='/home/apps_admin/sources/workflow';
buildsPath='/home/apps_admin/workflow_builds';

cd $gitPath;

port=7000;
branch='release';
version="v2."$(date +%y%m%d);

case "${1,,}" in
  sign) branch='esign'; port=7002; shift;;
  jan) branch='2022_jan'; port=7001; shift;;
  feb) branch='2022_feb'; port=7001; shift;;
  mar) branch='2022_mar'; port=7001; shift;;
  apr) branch='2022_apr'; port=7001; shift;;
  may) branch='2022_may'; port=7001; shift;;
  jul) branch='2022_jul'; port=7001; shift;;
  aug) branch='2022_aug'; port=7001; shift;;
  sep) branch='2021_sep'; port=7001; shift;;
  oct) branch='2021_oct'; port=7001; shift;;
  nov) branch='2021_nov'; port=7001; shift;;
  dec) branch='2021_dec'; port=7001; shift;;
esac

if [[ "${1,,}" = "-t" ]]; then
  git checkout -- * &> /dev/null;

  if git checkout $branch &> /dev/null; then
    echo "  switch to branch:${branch}...";
    git checkout -b $branch origin/$branch &> /dev/null;
    echo '  branch switch completed!';
  fi

  echo '  git pull...';
  git pull;
  echo '  git pull completed!';

  # netcore publish
  echo '  app build&publish...';
  rm -rf $buildsPath/* &> /dev/null;
  cd $gitPath/Src/src/IWorkFlow.Web;

  dotnet publish -c release -o $buildsPath &> /dev/null;
  if [ $? -ne 0 ]; then
    echo "  app build&publish error, please retry!";

    exit;
  fi

  chmod 777 -R $buildsPath;
  echo '  app build&publish completed!';

  # docker builder.
  cd $buildsPath;

  port=8888;
  docker rmi iworkflow_${branch,,}_$port &> /dev/null;
  docker build -t iworkflow_${branch,,}_$port . &> /dev/null;

  shift;
fi

if [[ "${1,,}" = "v2.2"* ]]; then
  fullVersion="$1";

  shift;
else
  counts=$(docker images | grep "10.164.2.213:4300/iworkflow/portal_web" | grep "$version" | awk '{print $3}' | wc -l);

  newImageId=$(docker images | grep iworkflow_${branch}_${port} | awk '{print $3}');
  oldImageId=$(docker images | grep 10.164.2.213:4300/iworkflow | grep $version.$counts | awk '{print $3}');

  fullVersion="$version.$counts";

  if [ "$newImageId" != "$oldImageId" ]; then
    counts=$[$counts + 1];
    fullVersion="$version.$counts";

    echo '  begin pull...';

    docker tag iworkflow_${branch}_${port} 10.164.2.213:4300/iworkflow/portal_web:$fullVersion &> /dev/null;
    docker push 10.164.2.213:4300/iworkflow/portal_web:$fullVersion;

    echo "  pull completed, the version is:${fullVersion}";
  fi
fi

deplyType="";

if [[ "${1,,}" = '--'* ]]; then
  deplyType="${1,,}";

  shift;
fi

if [ -n "$deplyType" ]; then
  case $deplyType in
    --local)
      echo "  begin deploy to local...";
      sh deploy.sh ${fullVersion} $*;;
    --uat) 
      echo "  begin deploy to uat...";
      sshpass -p 'C@pitaland123' ssh apps_admin@10.164.2.133 "sudo sh deploy.sh ${fullVersion} $*";;
    --prd) 
      echo "  begin deploy to production...";
      sshpass -p 'C@pitaland123' ssh apps_admin@10.164.5.160 "sudo sh deploy.sh ${fullVersion} $*";;
    *)
      echo "  error:not current oprator: $deplyType!";;
  esac
fi
